home *** CD-ROM | disk | FTP | other *** search
- ///--------------------------------------------------------------------------------------
- // Simple.c
- ///--------------------------------------------------------------------------------------
-
- #include <SWIncludes.h> // Automatically include all SpriteWorld headers
- #include <SWGameUtils.h>
- #include <SWFPSReport.h>
-
- #include "SWApplication.h"
- #include "Simple.h"
-
-
- #define kFullScreenWindow false // Set to true if you want a window larger than 640x480
- #define kInterlacedMode false // You can use interlacing even with non-scrolling animations
- #define kSyncToVBL true // Syncs the animation to the VBL
- #define kWorldRectInset 0 // Makes the SpriteWorld smaller than the window.
- #define kMaxFPS 0 // Keep the animation from going too fast
-
- #define kNumSprites 10
- #define kMaxSpriteMoveDelta 8
-
-
- SpriteWorldPtr gSpriteWorldP;
- SpriteLayerPtr gSpriteLayerP;
- SpritePtr gMasterBallSpriteP;
- WindowPtr gWindowP;
-
-
- ///--------------------------------------------------------------------------------------
- // main
- ///--------------------------------------------------------------------------------------
-
- void main(void)
- {
- Initialize(kNumberOfMoreMastersCalls);
- Randomize();
-
- if (SWHasSystem7())
- {
- CreateWindow();
- SetUpSpriteWorld();
- CreateBallSprite();
-
- AddSprites();
- RunAnimation();
- CleanUp();
- }
- else
- {
- CantRunOnThisMachine();
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // CreateWindow
- ///--------------------------------------------------------------------------------------
-
- void CreateWindow(void)
- {
- Rect windRect;
- RgnHandle mBarUpdateRgn;
-
- gWindowP = GetNewCWindow(kWindowResID, NULL, (WindowPtr)-1L);
-
- if (gWindowP != NULL)
- {
- if (kFullScreenWindow == true)
- {
- SizeWindow(gWindowP, qd.screenBits.bounds.right,
- qd.screenBits.bounds.bottom, false);
- MoveWindow(gWindowP, 0, 0, false);
- }
- else
- {
- MoveWindow(gWindowP, 0, 0, false);
- windRect = gWindowP->portRect;
-
- // Make sure the window fits on the screen
- if (windRect.bottom > qd.screenBits.bounds.bottom ||
- windRect.right > qd.screenBits.bounds.right)
- {
- // Make it smaller so it fits on the screen
- SizeWindow(gWindowP, qd.screenBits.bounds.right,
- qd.screenBits.bounds.bottom, false);
- MoveWindow(gWindowP, 0, 0, false);
- }
- else
- {
- // Center window in screen
- CenterRect(&windRect, &qd.screenBits.bounds);
- MoveWindow(gWindowP, windRect.left, windRect.top, false);
- }
- }
-
- ShowWindow(gWindowP);
- SetPort(gWindowP);
- mBarUpdateRgn = SWHideMenuBar(gWindowP); // Must be done *after* showing window!
- EraseRgn(mBarUpdateRgn);
- }
- else
- CantFindResource();
- }
-
-
- ///--------------------------------------------------------------------------------------
- // SetUpSpriteWorld
- ///--------------------------------------------------------------------------------------
-
- void SetUpSpriteWorld(void)
- {
- OSErr err;
- Rect worldRect;
- PixPatHandle pixPatH;
-
- SetCursor(*GetCursor(watchCursor));
-
- // Initialize the SpriteWorld package
- err = SWEnterSpriteWorld();
- FatalError(err);
-
- worldRect = gWindowP->portRect;
- InsetRect(&worldRect, kWorldRectInset, kWorldRectInset);
-
- // Create the SpriteWorld
- err = SWCreateSpriteWorldFromWindow(&gSpriteWorldP, (CWindowPtr)gWindowP,
- &worldRect, NULL, 0);
- FatalError(err);
-
- // Create the Sprite Layer
- err = SWCreateSpriteLayer(&gSpriteLayerP);
- FatalError(err);
-
- // Put the pieces together and lock the SpriteWorld.
- // Note: since we are locking the SpriteWorld before adding the sprites,
- // we must make sure to lock each sprite individually when creating them.
- SWAddSpriteLayer(gSpriteWorldP, gSpriteLayerP);
- SWLockSpriteWorld(gSpriteWorldP);
-
-
- // Use BlitPixie for the screen and offscreen DrawProcs.
- if (gSpriteWorldP->pixelDepth == 8) // 256 colors
- {
- if (kInterlacedMode)
- {
- SWSetSpriteWorldScreenDrawProc(gSpriteWorldP, BP8BitInterlacedRectDrawProc);
- SWSetSpriteWorldOffscreenDrawProc(gSpriteWorldP, BP8BitInterlacedRectDrawProc);
- }
- else
- {
- SWSetSpriteWorldScreenDrawProc(gSpriteWorldP, BlitPixie8BitRectDrawProc);
- SWSetSpriteWorldOffscreenDrawProc(gSpriteWorldP, BlitPixie8BitRectDrawProc);
- }
- }
- else if ( !(SW_PPC && gSpriteWorldP->pixelDepth < 8) ) // not 256 colors
- {
- if (kInterlacedMode)
- {
- SWSetSpriteWorldScreenDrawProc(gSpriteWorldP, BPAllBitInterlacedRectDrawProc);
- SWSetSpriteWorldOffscreenDrawProc(gSpriteWorldP, BPAllBitInterlacedRectDrawProc);
- }
- else
- {
- SWSetSpriteWorldScreenDrawProc(gSpriteWorldP, BlitPixieAllBitRectDrawProc);
- SWSetSpriteWorldOffscreenDrawProc(gSpriteWorldP, BlitPixieAllBitRectDrawProc);
- }
- }
-
- // Draw the background
- SWSetPortToBackground(gSpriteWorldP);
-
- if (gSpriteWorldP->pixelDepth == 1)
- pixPatH = GetPixPat(129); // B&W dithered background
- else
- pixPatH = GetPixPat(128); // Color
-
- if (pixPatH != NULL)
- {
- FillCRect(&gSpriteWorldP->backRect, pixPatH);
- DisposePixPat(pixPatH);
- }
-
- // Set up other miscellaneous options
- SWSetSpriteWorldMaxFPS(gSpriteWorldP, kMaxFPS);
- SWSyncSpriteWorldToVBL(gSpriteWorldP, kSyncToVBL);
- SWSetCleanUpSpriteWorld(gSpriteWorldP);
- }
-
-
- ///--------------------------------------------------------------------------------------
- // CreateBallSprite - load the master ball sprite and prepare it for cloning later
- ///--------------------------------------------------------------------------------------
-
- void CreateBallSprite(void)
- {
- OSErr err;
-
- err = SWCreateSpriteFromCicnResource(gSpriteWorldP, &gMasterBallSpriteP, NULL,
- 128, 1, kFatMask);
- FatalError(err);
-
- // Set up the sprite (remember that it must be locked!)
- SWLockSprite(gMasterBallSpriteP);
- SWSetSpriteMoveBounds(gMasterBallSpriteP, &gSpriteWorldP->backRect);
- SWSetSpriteMoveProc(gMasterBallSpriteP, BallSpriteMoveProc);
-
- if (gSpriteWorldP->pixelDepth == 8)
- {
- SWCompileSprite(gMasterBallSpriteP);
- SWSetSpriteDrawProc(gMasterBallSpriteP, CompiledSprite8BitDrawProc);
- }
- else if ( !(SW_PPC && gSpriteWorldP->pixelDepth < 8) )
- {
- if (kInterlacedMode)
- SWSetSpriteDrawProc(gMasterBallSpriteP, BPAllBitInterlacedMaskDrawProc);
- else
- SWSetSpriteDrawProc(gMasterBallSpriteP, BlitPixieAllBitMaskDrawProc);
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // AddSprites - clone the master sprite, and add the clones to the SpriteWorld
- ///--------------------------------------------------------------------------------------
-
- void AddSprites(void)
- {
- SpritePtr newSpriteP;
- short spriteNum;
- OSErr err;
-
- for (spriteNum = 0; spriteNum < kNumSprites; spriteNum++)
- {
- // Clone the master sprite. The clone doesn't need to be locked,
- // since the master sprite was already locked.
- err = SWCloneSprite(gMasterBallSpriteP, &newSpriteP, NULL);
- FatalError(err);
-
- SWAddSprite(gSpriteLayerP, newSpriteP);
-
- SWSetSpriteLocation(newSpriteP,
- GetRandom(0, gSpriteWorldP->backRect.right-44),
- GetRandom(0, gSpriteWorldP->backRect.bottom-44) );
-
- do
- {
- SWSetSpriteMoveDelta(newSpriteP,
- GetRandom(-kMaxSpriteMoveDelta, kMaxSpriteMoveDelta),
- GetRandom(-kMaxSpriteMoveDelta, kMaxSpriteMoveDelta));
- } while (newSpriteP->horizMoveDelta == 0 || newSpriteP->vertMoveDelta == 0);
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // RunAnimation
- ///--------------------------------------------------------------------------------------
-
- void RunAnimation(void)
- {
- unsigned long frames;
-
- SetCursor(&qd.arrow);
- HideCursor();
-
- // Make sure CopyBits, if used, doesn't try to colorize things
- SWSetPortToWindow(gSpriteWorldP);
- ForeColor(blackColor);
- BackColor(whiteColor);
-
- SWUpdateSpriteWorld(gSpriteWorldP, true);
-
- FatalError( SWStickyError() ); // Make sure no errors got past us during setup
-
- frames = 0;
- StartTimer(); // For FPS report later
-
- while (!Button())
- {
- SWProcessSpriteWorld(gSpriteWorldP);
- FatalError( SWStickyError() ); // Make sure no errors occurred during a MoveProc, etc.
- SWAnimateSpriteWorld(gSpriteWorldP);
-
- if (gSpriteWorldP->frameHasOccurred)
- frames++;
- }
-
- SWShowMenuBar((WindowPtr)gWindowP);
- ShowResults(frames); // Show FPS report
- }
-
-
- ///--------------------------------------------------------------------------------------
- // CleanUp - This function is not really necessary, since the system will dispose
- // everything automatically when the program quits. However, if you sync the animation
- // to the VBL, you must either call SWDisposeSpriteWorld, or SWSyncSpriteWorldToVBL
- // with a value of false before your program quits, in order to remove the VBL task.
- ///--------------------------------------------------------------------------------------
-
- void CleanUp(void)
- {
- // Dispose the master sprite, since it isn't included in the SpriteWorld
- SWDisposeSprite(&gMasterBallSpriteP);
-
- // Dispose the SpriteWorld, including all sprites and layers currently in it
- SWDisposeSpriteWorld(&gSpriteWorldP);
- SWExitSpriteWorld();
-
- FlushEvents(everyEvent, 0);
- ShowCursor();
- }
-
-
- ///--------------------------------------------------------------------------------------
- // BallSpriteMoveProc
- ///--------------------------------------------------------------------------------------
-
- SW_FUNC void BallSpriteMoveProc(SpritePtr ballSpriteP)
- {
- SWOffsetSprite(ballSpriteP, ballSpriteP->horizMoveDelta, ballSpriteP->vertMoveDelta);
-
- (void)SWBounceSprite(ballSpriteP);
- //(void)SWWrapSprite(ballSpriteP);
- }
-